/**
 * @description: 手机号登陆
 * @update: 2019-06-10
 * @version: 1.0.0
 **/

(function (win) {
    var LoginPhone = function (options) {
        var _default = {
            width: 200,
        };
        $.extend(_default, options);

        var html = "";
        html += "<div class='login_phone'>";
        html += "   <div class='img'><img src='http://i1.cy.com/cy/logo/cy_logo.png' /></div>";
        html += "   <div class='item phone'>";
        html += "       <input class='input_phone' type='number' placeholder='输入您的手机号，直接登录/注册' onblur='window.scrollTo(0, 0)' />";
        html += "       <span class='icon_cy icon_right iconzhengque'></span>";
        html += "       <span class='icon_cy icon_phone iconshouji'></span>";
        html += "       <p class='error phone_msg'></p>";
        html += "   </div>";
        html += "   <div class='item phone_code'>";
        html += "       <input class='input_phone_code' type='number' placeholder='短信随机码' onblur='window.scrollTo(0, 0)' />";
        html += "       <button class='btn_get_code disabled'>获取验证码</button>";
        html += "       <span class='icon_cy icon_phone_code iconyanzhengma'></span>";
        html += "       <p class='error phone_code_msg'></p>";
        html += "   </div>";
        html += "   <div class='btn'>";
        html += "       <button class='btn_login'>登录/一键注册</button>";
        html += "   </div>";
        html += "</div>";

        $("body").html(html);

        var time = 60
            , $inputPhone = $('.input_phone')
            , $inputPhoneCode = $('.input_phone_code')
            , $phoneMsg = $('.phone_msg')
            , $phoneCodeMsg = $('.phone_code_msg')
            , $phoneRight = $('.login_phone .icon_right')
            , $btnGetCode = $('.btn_get_code');

        // 手机号
        $inputPhone.keyup(function () {
            if ($(this).val().length > 11) $(this).val($(this).val().slice(0, 11));
            var valuePhone = $(this).val();
            if (!valuePhone) {
                $phoneMsg.text('请输入手机号').show();
                $phoneRight.hide();
                $btnGetCode.addClass('disabled')
            } else if (!valuePhone.match(/^1[3456789]\d{9}$/)) {
                $phoneMsg.text('请输入正确的手机号').show();
                $phoneRight.hide();
                $btnGetCode.addClass('disabled')
            } else {
                $phoneMsg.text('').hide();
                $phoneRight.show();
                $btnGetCode.removeClass('disabled')
            }
        });
        // 手机验证码
        $inputPhoneCode.keyup(function () {
            if ($(this).val().length > 6) $(this).val($(this).val().slice(0, 6));
            var valueCode = $(this).val();
            if (!valueCode) {
                $phoneCodeMsg.text('请输入验证码').show();
            } else if (valueCode.length !== 6) {
                $phoneCodeMsg.text('验证码为6位数字').show();
            } else {
                $phoneCodeMsg.text('').hide();
            }
        });
        // 获取验证码
        $btnGetCode.click(function () {
            $(this).text('正在发送...').addClass('disabled');
            jn.items.getCode($inputPhone.val(), function(res) {
                if(res.code === 10000) {
                    $(this).text(time + 's').addClass('disabled');
                }else {
                    $phoneCodeMsg.text(res.msg).remove('disabled');
                }
            })
        })
    };
    win.LoginPhone = LoginPhone;
})(window);